<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

include 'BipartiteGraph.php';
include 'Teacher.php';
include 'Discipline.php';
include 'core/Database.php';
include 'model/TeacherModel.php';
include 'model/DisciplineModel.php';
include 'model/Distribution.php';
include 'model/Weights.php';
include 'Process.php';



/*$weights = array(
    array(
        1,2,3,4
    ),
    array(
        3,4,5,6
    )
);
$b = new BipartiteGraph($weights);
$b->createFile("teste.txt");

$db = Database::singleton();

$b->printMatrix();
var_dump($db);

$process = new Process(array('teacher' => 'TeacherModel', 'discipline' => 'DisciplineModel'), "teste.xml");


$teacher = new TeacherModel();

$teachers = $teacher->find(array('condition' => array('_type' => 'professor'), 'order' => array('_id'), 'orderCondition' => 'ASC'));

var_dump($teacher);

$distribution = new SimpleXMLElement('<distribution name="Teste" year="2013" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="distribution.xsd" ></distribution>');

$teachersNode = $distribution->addChild('teachers');




$str ="";
foreach ($teachers as $professor) {
	$teacherNode = $teachersNode->addChild('teacher');
	$teacherNode->addChild('id', $professor['_user']['_id']);
	$semester = $teacherNode->addChild('semester');
	$semester->addAttribute('num', '1');
	$semester->addAttribute('available', '1');
	$semester = $teacherNode->addChild('semester');
	$semester->addAttribute('num', '2');
	$semester->addAttribute('available', '1');
	$teacherNode->addChild('workload', $professor['_user']['carga_horaria']);
	$teacherNode->addChild('Aworkload', $professor['_user']['ch_adicional']);
}

$relations = $distribution->addChild('relations');

/*$file = fopen('7531141', 'r');

while((fscanf($file, "%d %d %d", $professor, $disciplina, $fix)))
{
	$assignment = $relations->addChild('assignment');
	$assignment->addChild('teacher', $professor);
	$assignment->addChild('discipline', $disciplina);
	$assignment->addChild('fixed', $fix);
}


$distribution->asXML('teste.xml');


$teachers = $teacher->find(array('condition' => array('_type' => 'professor'), 'order' => array('_id'), 'orderCondition' => 'ASC'));

foreach ($teachers as $key => $value) {

}*/

/*$db = Database::singleton();

$st = $db->getConexao()->prepare('SELECT _name, "name", course, team, "_weight", semester, total_workload FROM "dct"."_teacher_disciplines" LEFT JOIN "_user" ON "_teacher_id" = "_id" LEFT JOIN "_disciplines" ON "_disciplines_id" = "id" WHERE NOT(_name ILIKE \'%temporario%\') AND NOT(_name ILIKE \'%colaborador%\');');
$st->execute();
$result = array();
foreach ($st->fetchAll() as $key => $value) {
	$result[$value['_name']][] = array('team' => $value['team'], 'name' => $value['name'], 'course' => $value['course'], 'semester' => $value['semester'], 'total' => $value['total_workload'], 'weight' => $value['_weight']);
}


$db = new Database(1);
foreach ($result as $key => $value) {
	$st = $db->getConexao()->prepare('SELECT id FROM teachers WHERE name = \''.utf8_encode($key).'\'');
	$st->execute();
	$id = $st->fetchAll();

	if($id)
	{
		echo $id[0]['id']."<br />";
		foreach ($value as $discipline) {
			if($discipline['team'])
			{
				$stm = $db->getConexao()->prepare('SELECT id FROM disciplines WHERE name = \''.utf8_encode($discipline['name']).'\' AND team = \''.utf8_encode($discipline['team']).'\' AND semester = \''.utf8_encode($discipline['semester']).'\' AND total_workload = \''.utf8_encode($discipline['total']).'\' AND course = \''.utf8_encode($discipline['course']).'\'');
				$stm->execute();
				$dis = $stm->fetchAll();
				if($discipline['weight'])
				{
					$stm = $db->getConexao()->prepare('INSERT INTO disciplines_teachers(teacher_id, discipline_id, weight) VALUES('.$id[0]['id'].', '.$dis[0]['id'].', '.$discipline['weight'].')');
					$stm->execute();
				}
				
			}
			
			
		}
	}
}*/

?>
